package com.yb.web.tribune;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.tribune.TribuneFlowService;
import com.yb.bussiness.tribune.TribuneFlowServiceImpl;
import com.yb.bussiness.tribune.TribuneService;
import com.yb.bussiness.tribune.TribuneServiceImpl;
import com.yb.domain.member.TsSysuser;
import com.yb.domain.tribune.Tribune;
import com.yb.domain.tribune.TribuneFlow;
import com.yb.domain.tribune.TribuneMaster;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

/**
 * TribuneFlowAction entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class TribuneFlowAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private List<TribuneFlow> triFlowList;
	private TribuneFlow triFObj;
	private Tribune triObj;
	private TribuneService tservice;
	private TribuneFlowService service;
	private String flag;
	
	public TribuneFlowAction() {
		tservice = new TribuneServiceImpl();
		service = new TribuneFlowServiceImpl();
	}
	
	public String addFlowtribune(){
		TsSysuser  tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
		try {
			Tribune tri = tservice.getById(triFObj.getTid().toString());
			tri.setLastVisitTime(new Date());
			try {
				tri.setFlowCount(tri.getFlowCount()+1);
			} catch (Exception e) {
				tri.setFlowCount(tri.getFlowCount());
			}
			tservice.saveOrUpdate(tri);
			triFObj.setCreateTime(new Date());
			triFObj.setAuthorId(tsSysuser.getId());
			triFObj.setAuthorName(tsSysuser.getUsername());
			service.saveOrUpdate(triFObj);
			int pageno = RequestUtil.getParam(request, "pageno", 1);
			int limit = RequestUtil.getParam(request, "limit", 20);
			triObj = tservice.getById(triFObj.getTid().toString());
			PageUtil pu = service.getListById(triFObj.getTid().toString(),pageno, limit);
			ActionContext.getContext().put("pu", pu);
			return "viewPage";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "viewPage";
	}

	/**
	 * 送鲜花
	 * @return
	 */
	public String sendFlower(){
		try {
			String id = RequestUtil.getParam(request, "id", "0");
			triFObj = service.read(Long.valueOf(id));
			if(null!=triFObj){
				triFObj.setFlowers(triFObj.getFlowers()+1);
				service.saveOrUpdate(triFObj);
				response.getWriter().print("1");
			}else
				response.getWriter().print("2");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 送鸡蛋
	 * @return
	 */
	public String sendEgg(){
		try {
			String id = RequestUtil.getParam(request, "id", "0");
			triFObj = service.read(Long.valueOf(id));
			if(null!=triFObj){
				triFObj.setEggs(triFObj.getEggs()+1);
				service.saveOrUpdate(triFObj);
				response.getWriter().print("1");
			}else
				response.getWriter().print("2");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 删除跟帖
	 * @return
	 */
	public String deleteFlowerTie() {
		String idString = RequestUtil.getParam(request, "idString", "");
		boolean isFlag = service.deleteFlowerTie(idString);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		if (isFlag)
			message = "删除成功";
		else
			message = "删除失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}
	
	public List<TribuneFlow> getTriFlowList() {
		return triFlowList;
	}

	public void setTriFlowList(List<TribuneFlow> triFlowList) {
		this.triFlowList = triFlowList;
	}

	public TribuneFlow getTriFObj() {
		return triFObj;
	}

	public void setTriFObj(TribuneFlow triFObj) {
		this.triFObj = triFObj;
	}

	public Tribune getTriObj() {
		return triObj;
	}

	public void setTriObj(Tribune triObj) {
		this.triObj = triObj;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

}